2022-05-16

What is gganimate? x

  • Extension package used with ggplot2

  • Used to make animations using ggplots

    • Can also save rendered plots as gifs

Why use gganimate?

  • Offers simple and easy to implement functions for adding animations
  • Can improve visualization of change in data
  • Adds to presentation of data

gganimate Basics & Examples

  • gganimate is primarily applied to ggplot objects by specifying transitions to use
  • There are 4 primary transition functions that will be covered:
    • transition_time()
    • transition_reveal()
    • transition_filter()
    • transition_states()

Getting Started

Getting started with gganimate could not be easier!

To start, create a regular ggplot. Then, add a transition layer based on what you want to change

transition_time()

  • transition_time() is useful for showing differences in variables over time
  • This is a very intuitive and interesting way to show time
  • You can also make time a third variable in a two-dimensional graph

Let’s look at the example from the last slide

transition_time() Example

This is not very clear, since the trends are very hard to see

Let’s fix that!

shadow_trail()

  • shadow_trail and will keep points behind the moving value, called a shadow
  • This allows the trends to be easier to see

shadow_trail() Example

transition_time() vs transition_reveal()

  • The plot on the left shows all the seasons in NBA history in which a player averaged at least 30 points per game
  • On the right the plot is animated using transition_time()

  • Once again, the animation does not communicate much since the points disappear very quickly

transition_time() vs transition_reveal()

  • Instead of using shadow_trails, we can see what the whole plot looks like using transition_reveal()
  • The animation on the left uses transition_time() and the animation on the right uses transition_reveal()

- transition_reveal() does a much better job of communicating the data than transition_time() on its own

shadow_wake()

  • The animation can still be improved though. Any player that occurs on the list multiple times is only on the plot once
  • Using the shadow_wake() function of gganimate, we can add a wake to these players to show their scoring over their career
  • shadow_wake() is similar to shadow_trail(), but the points eventually fade
  • The amount of time the points stay visible can be set with wake_length

shadow_wake()

  • Here are the animations side by side

- Shadow_wake() communicates who the most productive players were over the course of their career instead of just a single season

transition_filter()

  • The plot below shows all the cars in mtcars on a scatter plot with miles per gallon on the x-axis and horsepower on the y-axis
  • This plot does not communicate much aside from the relationship between horsepower and miles per gallon
  • Some simple animation could improve this plot

transition_filter()

  • transition_filter() creates an animation that cycles through different filters of the data
  • We can use other variables from mtcars other than hp and mpg to set filters
  • This can be used as an alternative to faceting

tranistion_filter()

  • Once the transition_filter() function is applied our static plot is transformed into a more informative animation
  • The animation communicates trends within certain filters that would not be shown in a single, static plot

transition_states()

  • transition_states() is useful for non-time related transitions between data
  • Also can make use of other fields or options to improve animation

So, let’s look at an example

transition_states() Example(1)

  • Suppose we look at some simulated data of a BJT transistor circuit component:
  • The output behavior of the component generally looked at is voltage vs current
  • In this case, that would be Vce vs. Ic

transition_states() Example(2)

  • The output function also depends on an input current Ib, however, and varies accordingly
  • As such, we get the following ggplot

transition_states() Example(3)

  • Showing the data in this format indicates the behavior, but we can improve it is communicated
  • Applying state transitions to the Ib value, the behavior can then be made into the following animated plot

transition_states() Example(4)

  • This better indicates how the function output behavior increases as Ib increases
  • Furthermore, making use of shadow_mark(), we can then add onto the existing plot
    • shadow_mark() is similar to shadow_trail(), however it leaves a mark at the data state

Customization

  • Animate()
    • takes a gganim object and renders it into an animation
    • gives the ability to customize the animated plot
    • takes arguments like fps, duration, ref_frame, start_pause, end_pause and rewind
  • Labs()
    • Allows you to customize your animated plot with a title, x label and y label

Let’s look at an example!

Cars Data Set

  • Here is a basic gganimate plot

With Animate and Labs

  • Now lets utilize some extra functions we learned
  • Animate() allows us to change things about the animated plot like hight, width and fps
  • Labs() allows us to put in a good title

Conclusion

  • gganimate unlocks the potential for countless designs and visualizations of different graphs
  • can be the difference between a successful presentation and an unsccessful one
  • allows for pretty and unique custom plots
  • relatively easy - not many lines of code

Questions?